package com.chen.script.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import com.chen.base.utils.BashUtil;
import com.chen.root.base.R;
import com.chen.script.config.CommonProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenJinLong
 */
@RestController
@RequestMapping("/sys")
@Api(value = "sys", tags = "系统API")
public class SystemController {

    @Resource
    private CommonProperties commonProperties;

    @PostMapping("/updateSystemDateTime")
    @ApiOperation(value = "更新系统时间",notes = "更新系统时间")
    public R<Boolean> updateSystemDateTime(String date, String time) throws Exception {
        List<String> commandList = new ArrayList<>();
        commandList.add("@echo off");
        commandList.add("%1 mshta vbscript:CreateObject(\"Shell.Application\").ShellExecute(\"cmd.exe\",\"/c %~s0 ::\",\"\",\"runas\",1)(window.close)&&exit");
        commandList.add("time " + time);
        commandList.add("date " + date);
        String batFilePath = commonProperties.getTempFileDir() + "\\updateWinDateTime.bat";
        FileUtil.writeLines(commandList, batFilePath, CharsetUtil.CHARSET_UTF_8, false);
        BashUtil.execBatScript(batFilePath);
        return R.ok(true);
    }

}
